import express from 'express'

// const hostModifier = require('./host.js');
import hostModifier from './host.js'
// 读取 hosts 文件内容
// const hostsContent = hostModifier.readHostsFile();
// if (hostsContent) {
//     console.log('Current hosts file content:\n', hostsContent);
// }

// 修改 hosts 文件内容
// const newIp = '127.0.0.1';
// const newHostname = 'example.com';
// const success = hostModifier.modifyHostsFile(newIp, newHostname);
// if (success) {
//     console.log(`Modified hosts file: Added ${newIp} ${newHostname}`);
// } else {
//     console.log('Failed to modify hosts file');
// }
hostModifier.start()

// 成功数据加工
const successData = res => {
    return JSON.stringify({
        code: 200,
        data: res
    })
}
// 启用服务
const app = express()
// 获取本地host
app.get('/host', (req, res) => {
    const data = hostModifier.readHostsFile()
    res.end(successData(data))
})
// 获取配置
app.post('/userOption', (req, res) => {
    const { params, query, cookies, headers } = req
    // console.log('userOption', { params, query, cookies, headers });
    const data = hostModifier.getOption()
    res.end(successData(data))
})
const server = app.listen(8081, () => {
    let host = server.address().address
    let port = server.address().port

    console.log("应用实例，访问地址为 http://%s:%s", host, port)
})


/**
 * 业务代码
 */

// 查询本地配置文件
// 读取host配置
// 对比存储数据更新host
// 启动代理服务